import {
  hasChild,
  localRead
} from '@/libs/util'
import Main from '@/components/main'
import ParentView from '@/components/parent-view'
import {
  forEach
} from '@/libs/tools'

//所有页面信息
const constantRouterComponents = {
  "home": () => import('@/view/single-page/home'),
  "join_page": () => import('@/view/join-page.vue'),
  "message_page": () => import('@/view/single-page/message/index.vue'),
  "tree_select_page": () => import('@/view/components/tree-select/index.vue'),
  "count_to_page": () => import('@/view/components/count-to/count-to.vue'),
  "drag_list_page": () => import('@/view/components/drag-list/drag-list.vue'),
  "drag_drawer_page": () => import('@/view/components/drag-drawer'),
  "org_tree_page": () => import('@/view/components/org-tree'),
  "tree_table_page": () => import('@/view/components/tree-table/index.vue'),
  "cropper_page": () => import('@/view/components/cropper/cropper.vue'),
  "tables_page": () => import('@/view/components/tables/tables.vue'),
  "split_pane_page": () => import('@/view/components/split-pane/split-pane.vue'),
  "markdown_page": () => import('@/view/components/markdown/markdown.vue'),
  "editor_page": () => import('@/view/components/editor/editor.vue'),
  "icons_page": () => import('@/view/components/icons/icons.vue'),
  "update_table_page": () => import('@/view/update/update-table.vue'),
  "update_paste_page": () => import('@/view/update/update-paste.vue'),
  "upload-excel": () => import('@/view/excel/upload-excel.vue'),
  "export-excel": () => import('@/view/excel/export-excel.vue'),
  "tools_methods_page": () => import('@/view/tools-methods/tools-methods.vue'),
  "i18n_page": () => import('@/view/i18n/i18n-page.vue'),
  "error_store_page": () => import('@/view/error-store/error-store.vue'),
  "error_logger_page": () => import('@/view/single-page/error-logger.vue'),
  "directive_page": () => import('@/view/directive/directive.vue'),
  "level_2_1": () => import('@/view/multilevel/level-2-1.vue'),
  "level_2_3": () => import('@/view/multilevel/level-2-3.vue'),
  "level_2_2_1": () => import('@/view/multilevel/level-2-2/level-2-2-1.vue'),
  "level_2_2_2": () => import('@/view/multilevel/level-2-2/level-2-2-2.vue'),
  "params": () => import('@/view/argu-page/params.vue'),
  "query": () => import('@/view/argu-page/query.vue')
}

// 加载菜单
export const loadMenu = () => {
  let list = []
  let data = localRead('route')
  if (!data) {
    return list
  }
  list = formatMenu(JSON.parse(data))
  return list
}

/**
 * 格式化菜单
 * @param list
 * @returns {[]}
 */
export const formatMenu = (list) => {
  let res = []
  forEach(list, item => {
    let route = new Object();
    if (item.data.activeMenu) {
      route.activeMenu = item.data.activeMenu
    }
    if (item.data.affix) {
      route.affix = item.data.affix
    }
    if (item.data.alwaysShow) {
      route.alwaysShow = item.data.alwaysShow
    }
    route.id = item.data.id
    route.meta = item.data.meta || []
    if (item.data.noCache) {
      route.noCache = item.data.noCache
    }
    if (item.data.path.startsWith("http") || item.data.path.startsWith("https") || item.data.path.startsWith("/")) {
      route.path = item.data.path
    } else {
      route.path = item.data.path
    }
    if (item.data.redirect) {
      route.redirect = item.data.redirect
    }
    if (item.data.title) {
      route.title = item.data.title
    }
    if (item.data.type) {
      route.type = item.data.type
    }
    if (item.href) {
      route.href = item.data.href
    }
    if (item.data.name) {
      route.name = item.data.name
    } else if (item.data.path.startsWith("http") || item.data.path.startsWith("https")) {
      route.name = null
    } else if (route.path.startsWith("/")) {
      route.name = route.path.slice(1).replaceAll("/", "-")
    } else {
      route.name = route.path.replaceAll("/", "-")
    }
    if (item.data.component) {
      if (item.data.component == 'Main') {
        route.component = Main
      } else if (item.data.component == 'parentView') {
        route.component = ParentView
      } else {
        route.component = constantRouterComponents[item.data.name]
      }
    }
    if (hasChild(item)) {
      route.children = formatMenu(item.children)
    }
    res.push(route)
  })
  return res
}
